package itsm.isperp.framework.web.utils;

import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.mybatis.plugin.JqGridResponseData;
import itsm.isperp.framework.web.request.JqGridRequest;

import java.util.List;

import org.activiti.engine.query.Query;

/**
 * web数据请求工具类
 * 
 * @author lizx
 * @date 2013-3-19
 */
public final class DataRequestUtils {

	/**
	 * 根据JqGrid请求以及查询器，返回web查询的数据分页结果
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public static final <T extends Query<?, ?>, U extends Object> DataResponse<U> buildFormQuery(
			JqGridRequest request, Query<T, U> query) {

		int firstResult = request.getOffset();
		List<U> list = query.listPage(firstResult,
				firstResult + request.getPageSize());

		long records = query.count();

		int total = (int) records / request.getPageSize();
		total = total == 0 ? 1 : total;

		return new JqGridResponseData<U>(list, request, total);

	}

	public static final String returnEmptyResponse() {
		return "[]";
	}
}
